/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.shared.net;

/**
 * @author Kristopher T Babic
 */
public class NetTransport {

    public static final char INIT = '#';

    public static final String SEPERATOR = "#|";
    public static final String START = "#@";
    public static final String END = "#!";
    public static final String CMD = "#C";
    public static final String USER = "#U";
    public static final String DATA = "#D";
    public static final String IMAGE = "#I";
    public static final String TIME = "#T";

    public static final int MAX_LEN = 40;

    public static final char CMD_GET_UID = (char) 0;
    public static final char CMD_GET_ALL = (char) 1;
    public static final char CMD_GET_TIME = (char) 2;
    public static final char CMD_GET_NEW = (char) 3;
    public static final char CMD_ADD = (char) 4;
    public static final char CMD_CREATE = (char) 5;
    public static final char CMD_DATA = (char) 6;
    public static final char CMD_GET_IMAGES = (char) 7;
    public static final char ERROR = (char) 8;
    public static final char OK = (char) 9;

    public static String getUID(String s) {
        if (s == null || s.length() == 0)
            return null;

        int index = s.indexOf(USER);

        if (index == -1)
            return null;

        int i = index - 1;
        int start = 0;
        for (i = index - 1; i >= 0; i--) {

            if (s.charAt(i) == INIT && i != 0 && s.charAt(i - 1) != INIT) {
                start = i + 2;
                break;
            }
            else if (i != 0 && s.charAt(i) == INIT && s.charAt(i - 1) == INIT)
                i -= 2;
            else if (i == 0 && s.charAt(i) == INIT && s.length() > 1 && s.charAt(i + 1) != INIT)
                start = i + 2;

        }

        return unstuff(s.substring(start, index));
    }

    public static String getImageName(String s) {
        if (s == null || s.length() == 0)
            return null;

        int index = s.indexOf(IMAGE);

        if (index == -1)
            return null;

        int i = index - 1;
        int start = 0;
        for (i = index - 1; i >= 0; i--) {

            if (s.charAt(i) == INIT && i != 0 && s.charAt(i - 1) != INIT) {
                start = i + 2;
                break;
            }
            else if (i != 0 && s.charAt(i) == INIT && s.charAt(i - 1) == INIT)
                i -= 2;
            else if (i == 0 && s.charAt(i) == INIT && s.length() > 1 && s.charAt(i + 1) != INIT)
                start = i + 2;

        }

        return s.substring(start, index);

    }

    public static String getData(String s) {
        if (s == null || s.length() == 0)
            return null;

        int index = s.indexOf(DATA);

        if (index == -1)
            return null;

        int i = index - 1;
        int start = 0;
        for (i = index - 1; i >= 0; i--) {

            if (s.charAt(i) == INIT && i != 0 && s.charAt(i - 1) != INIT) {
                start = i + 2;
                break;
            }
            else if (i != 0 && s.charAt(i) == INIT && s.charAt(i - 1) == INIT)
                i -= 2;
            else if (i == 0 && s.charAt(i) == INIT && s.length() > 1 && s.charAt(i + 1) != INIT)
                start = i + 2;

        }
//		System.out.println(">"+s.substring(start,index));
//		System.out.println(">>"+unstuff(s.substring(start,index)));	
        return unstuff(s.substring(start, index));

    }

    //changed from char
    public static char getCmd(String s) {
        if (s == null || s.length() == 0)
            return (char) -1;

        int index = s.indexOf(CMD);

        if (index == -1)
            return (char) -1;

        int i = index - 1;
        int start = 0;
        for (i = index - 1; i >= 0; i--) {

            if (s.charAt(i) == INIT && i != 0 && s.charAt(i - 1) != INIT) {
                start = i + 2;
                break;
            }
            else if (i != 0 && s.charAt(i) == INIT && s.charAt(i - 1) == INIT)
                i -= 2;
            else if (i == 0 && s.charAt(i) == INIT && s.length() > 1 && s.charAt(i + 1) != INIT)
                start = i + 2;

        }

//		System.out.println("start = "+start);
        return unstuff(s.substring(start, index)).charAt(0);

    }

    public static String createCommand(String s) {
        s = stuff(s) + CMD;
        return s;
    }

    public static String createCommand(char c) {
        String s = new String();
        s += c;
        s = stuff(s) + CMD;
        return s;
    }

    public static String createDataString(String s) {
//	System.out.println("<"+s);

        s = stuff(s) + DATA;
//	System.out.println("<<"+s);

        return s;
    }

    public static String createImageName(String s) {
        s = stuff(s) + IMAGE;
        return s;
    }

    public static String createUID(String s) {
        s = stuff(s) + USER;
        return s;
    }

    /* assumming already stuffed */
    public static String appendData(String data, String addData) {
        return data + SEPERATOR + stuff(addData);
    }

    public static java.util.Enumeration parseData(String dataString) {
        if (dataString == null || dataString.length() == 0)
            return null;

        java.util.Vector v = new java.util.Vector();

        int index = dataString.indexOf(SEPERATOR);
        //String toAdd = new String();

        while (index != -1) {
            v.add(dataString.substring(0, index));
            dataString = dataString.substring(index + 2);
            index = dataString.indexOf(SEPERATOR);
        }

        if (!dataString.equals(""))
            v.add(unstuff(dataString));

        return v.elements();
    }

    public static String stuff(String in) {

        String retStr = new String();
//System.out.println("********"+in.length()+"**********");
        int j = 0;
        for (int i = 0; i < in.length(); i++) {
            char ch = in.charAt(i);
            if (ch == INIT) {
                retStr += ch;
                j++;
            }

            retStr += ch;
        }
//System.out.println("**********"+retStr.length()+"**********"+j+"*******");
        return retStr;
    }

    public static String unstuff(String in) {

        String retStr = new String();
        int len = in.length();

        for (int i = 0; i < len; i++) {
            if (in.charAt(i) == INIT)
                i++;

            if (i < len)
                retStr += in.charAt(i);

        }

        return retStr;

    }
}